package com.lagou.handler;

import com.lagou.encode.RpcEncoder;
import com.lagou.request.RpcRequest;
import com.lagou.serializer.JSONSerializer;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;

public class StringChannelHandler extends ChannelInitializer<SocketChannel> {

    public StringChannelHandler(UserClientHandler userClientHandler) {
        StringChannelHandler.userClientHandler = userClientHandler;
    }

    // 2.声明一个自定义事件处理器 UserClientHandler
    private static UserClientHandler userClientHandler;

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        // 获取ChannelPipeline
        ChannelPipeline pipeline = socketChannel.pipeline();
        // 设置编码
        pipeline.addLast(new RpcEncoder(RpcRequest.class, new JSONSerializer()));
        pipeline.addLast(new StringDecoder());
        // 添加自定义事件处理器
        pipeline.addLast(userClientHandler);
    }
}
